﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Blogger.Controllers
{
	using NewtonJson = Newtonsoft.Json;

    public class HelloWorldController : Controller
    {
        public ActionResult Index()
        {
			TempData ["LastVisitedTime"] = DateTime.Now.ToString();
            return View ();
        }

		// 
		// GET: /HelloWorld/Welcome/ 

		public ActionResult Welcome(string name, int numTimes = 1) 
		{ 
			var welcomeTitle = "This is the Welcome action method...";
			var welcomeWords = string.Format(
				"For {0}, NumTimes is: {1}",
				name,
				numTimes); 

			if (TempData ["LastVisitedTime"] == null) 
			{
				TempData ["LastVisitedTime"] = "No Temp Data";
			}

			ViewData ["WelcomeTitle"] = welcomeTitle;
			ViewData ["WelcomeWords"] = welcomeWords;
			ViewData ["LastVisitedTime"] = TempData ["LastVisitedTime"];
			return View ();
		}

		public ActionResult Movie()
		{
			ViewData ["Movies"] = 
				new SelectList (
				Blogger.Models.Spark.Movie.RetriveDataSet (),
				"ID",
				"Title");

			return View ();
		}

		public JsonResult RefreshMovie()
		{
			return Json (
				Blogger.Models.Spark.Movie.RetriveDataSet (),
				JsonRequestBehavior.AllowGet);
		}

		[HttpPost]
		public JsonResult InsertMovie(string movies)
		{
			try {
				// var ser = new Newtonsoft.Json.JsonSerializer();
				var mv = (List<Dictionary<string, string>>)NewtonJson.JsonConvert.DeserializeObject(
					movies, 
					typeof(List<Dictionary<string, string>>));
				return Json(new {success="recieved " + mv.Count + " movie(s)"});

			} catch (Exception ex) {
				return Json(new {success="", error=ex.Message});
			}
		}
    }
}
